<template>
  <el-card class="box-card">
    <template #header>
      <div class="card-header">
        <span>应用管理</span>
      </div>
    </template>

    <div class="search-form">
      <div class="toobar-button">
        <el-button type="primary" @click="addApplication">
          <el-icon style="margin-right: 6px;">
            <Operation />
          </el-icon>
          新增应用
        </el-button>
      </div>
      <el-form :inline="true" :model="formInline" class="demo-form-inline" @submit.native.prevent="() => onSubmit">
        <el-form-item>
          <el-input placeholder="请输入应用名称" v-model="formInline.para" clearable @keyup.enter="onSubmit">
            <template #suffix>
              <el-icon class="el-input__icon curP"  @click="onSubmit"><Search /></el-icon>
            </template>
          </el-input>
        </el-form-item>
      </el-form>
      
    </div>

    <div class="table-data" v-loading="loading">
      <el-table
        :data="applicationData"
        stripe
        borderresizable
        highlight-hover-row
        show-overflow
        keep-source
        scrollbar-always-on
      >
        <el-table-column
          v-for="(tableItem, tableIndex) in applicationTableColumns" :key="tableIndex"
          :prop="tableItem.field"
          :label="tableItem.label"
          :min-width="tableItem.minWidth"
          :formatter="tableItem.formatter"
          show-overflow-tooltip
        >
        </el-table-column>
        <el-table-column
          fixed="right"
          field="opera"
          label="操作"
          min-width="120"
        >
          <template #default="{ row }">
            <el-button
              type="warning"
              link
              v-if="row.status == 1"
              @click="applicationChange(row)"
              :disabled="buttonDisabled"
            >禁用</el-button>
            <el-button
              type="warning"
              link
              v-if="row.status == 0"
              @click="applicationChange(row)"
              :disabled="buttonDisabled"
            >恢复</el-button>
            <el-button
              link
              @click="applicationDetail(row)"
              style="color: #5897DB;"
              :disabled="buttonDisabled"
            >查看详情</el-button>
            <el-button
              link
              @click="applicationEdit(row)"
              style="color: #5897DB;"
              :disabled="buttonDisabled"
            >编辑</el-button>
          </template>
        </el-table-column>
      </el-table>

      <div class="table-page">
        <el-pagination background layout="prev, pager, next" :page-count="dataTotal" :page-size="pageSize" :current-page="currentPage" @current-change="currentChange" />
      </div>
    </div>
  </el-card>
</template>
<script setup name="application">
import { useRouter } from 'vue-router';
import { computed, ref, onMounted } from 'vue';
import { Search, Operation } from '@element-plus/icons-vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { http } from '@/utils/http/index';
import { format } from '@/plugins/moment.js'

/* 路由 */
const router = useRouter()

onMounted(() => {
  onSubmit()
})

/* 检索表单 */
const formInline = ref({
  para: "",
})

/* 分页 */
const dataTotal = ref(0)
const pageSize = ref(15)
const currentPage = ref(1)

/* 切换页码方法 */
const currentChange = (val) => {
  currentPage.value = val
  onSubmit()
}

/* 表格数据加载动画 */
const loading = ref(false)

/* 表格数据 */
const applicationData = ref([])

/* 状态字段过滤 */
const statusFormatter = (row, column, cellValue) => {
  return cellValue == "1" ? "正常" : "停用"
}

/* 创建时间字段过滤 */
const getCreateTime = (cellValue) => {
  return format(cellValue)
}

/* 表格字段 */
const applicationTableColumns = ref([
  { field: "name", label: "应用名称", minWidth: "120" },
  { field: "id", label: "APPId", minWidth: "120" },
  { field: "authDomain", label: "授权地址", minWidth: "120" },
  { field: "createTime", label: "创建时间", minWidth: "120", formatter:({cellValue})=>{return getCreateTime(cellValue)} },
  { field: "status", label: "状态", minWidth: "120", formatter: statusFormatter },
])

/* 列表筛选 */
const onSubmit = () => {
  loading.value = true
  let params = {
    pagenum: currentPage.value,
    pagesize: pageSize.value,
    para: formInline.value.para
  }
  http.$GET("/AppManager/GetApp", params).then(res => {
    loading.value = false
    if (res.isError) return ElMessage.warning(res.message)
    applicationData.value = res.result.content
    dataTotal.value = res.result.num
  }).catch(() => {
    loading.value = false
    ElMessage.warning("查询失败")
  })
}

/* 点击新建 */
const addApplication = () => {
  router.push({
    path: "/AppManager/AppEdit",
    query: { type: 'add' }
  })
}

/* 点击编辑 */
const applicationEdit = (row) => {
  router.push({
    path: "/AppManager/AppEdit",
    query: { type: 'edit', id: row.id }
  })
}

/* 操作列按钮禁用 */
const buttonDisabled = ref(false)

/* 点击更改状态 */
const applicationChange = (row) => {
  ElMessageBox.confirm(
    `确定更改此条数据的状态吗？`,
    '请确认',
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning',
    }
  ).then(() => {
    buttonDisabled.value = true
    let params = {
      id: row.id,
      status: ""
    }
    if(row.status == 0) {
      params.status = 1
    } else {
      params.status = 0
    }

    const formData = new FormData()
    for(let k in params) {
      formData.append(k ,params[k])
    }
    http.$POST("/AppManager/EditAppStatus", formData).then(res => {
      buttonDisabled.value = false
      if(res.isError) return ElMessage.warning(res.message)
      onSubmit()
    }).catch(() => {
      buttonDisabled.value = false
      ElMessage.warning("操作失败")
    })
  }).catch(() => {
    ElMessage.info("已取消更改状态操作")
  })
}

/* 点击查看详情 */
const applicationDetail = (row) => {
  let { href } = router.resolve({
    path: "/AppManager/AppDetail",
    query: { id: row.id }
  })
  window.open(href, "_blank")
}

</script>
<style lang='scss' scoped>
@import "@/assets/styles/backendElement.scss";
</style>